<template>
  <div class="search-container">
    <!-- 在 van-search 外层增加 form 标签，且 action 不为空，即可在 iOS 输入法中显示搜索按钮。 -->
    <form action="/" class="search-from">
      <van-search
        v-model="searchText"
        show-action
        placeholder="请输入搜索关键词"
        background="#3296fa"
        @search="onSearch"
        @cancel="onCancel"
        @focus="isResultShow = false"
      />
    </form>
    <!-- 搜索结果 -->
    <search-result v-if="isResultShow" :search-text="searchText" />
    <!-- 联想结果 -->
    <search-suggestion
      v-else-if="searchText"
      :search-text="searchText"
      @search="onSearch"
    />
    <!-- 搜索历史 -->
    <search-history v-else />
  </div>
</template>

<script>
import searchHistory from "./components/search-history.vue";
import searchSuggestion from "./components/search-suggestion.vue";
import searchResult from "./components/search-result.vue";
export default {
  name: "SearchIndex",
  data() {
    return {
      searchText: "",
      isResultShow: false // 控制搜索结果的显示状态
    };
  },
  methods: {
    onSearch(val) {
      this.searchText = val;
      // 按下搜索按钮，显示搜索结果
      this.isResultShow = true;
    },
    onCancel() {
      this.$router.back();
    }
  },
  components: {
    searchHistory,
    searchSuggestion,
    searchResult
  }
};
</script>

<style scoped lang="less">
.search-container {
    padding-top: 108px;
  .van-search__action {
    color: #fff;
  }
  .search-from {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      z-index: 1;
  }
}
</style>
